﻿using EnterpriseWebsiteUtil.Attributes;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Primitives;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EnterpriseWebsiteApi.ActionFilters
{
    public class LanguageHeaderFilter : IActionFilter
    {
        public void OnActionExecuting(ActionExecutingContext context)
        {
            StringValues languageHeaderValues;
            if (context.HttpContext.Request.Headers.TryGetValue("language", out languageHeaderValues))
            {
                LangType langType = LangType.zh;
                string language = languageHeaderValues.ToString().ToLower();

                if (!Enum.TryParse(language, out langType))
                {
                    langType = LangType.zh;
                }

                context.ActionArguments["language"] = langType;
            }
            else
            {
                context.ActionArguments["language"] = LangType.zh;
            }
        }

        public void OnActionExecuted(ActionExecutedContext context)
        {
            
        }
    }
}
